package com.baijiaoxi.utils.qcloud;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.BusinessCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.BusinessCardOCRResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TencentOCRHelper {
    private String secretId = "AKID1oOkpe7UVGleNr44tSXLs75qKSK2pekZ";
    private String secretKey = "Yv1bx5wFIp9wSigOr52i49wTjkUx97oE";
    private Credential credential;


    public TencentOCRResult doOCR(String data) {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey
            // 实例化要请求产品(以cvm为例)的client对象
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
            OcrClient client = new OcrClient(credential, "ap-shanghai", clientProfile);
            // 实例化一个请求对象
            BusinessCardOCRRequest req = new BusinessCardOCRRequest();
            if (data.startsWith("http"))
                req.setImageUrl(data);
            else req.setImageBase64(data);
            // 通过client对象调用想要访问的接口，需要传入请求对象
            BusinessCardOCRResponse resp = client.BusinessCardOCR(req);
            return new TencentOCRResult(resp.getBusinessCardInfos());
        } catch (TencentCloudSDKException e) {
            log.error("ocr名片识别失败", e);
            return null;
        }
    }


    public TencentOCRHelper() {
        this.credential = new Credential(secretId, secretKey);
    }

    public TencentOCRHelper(String secretId, String secretKey) {
        this.secretId = secretId;
        this.secretKey = secretKey;
        this.credential = new Credential(secretId, secretKey);
    }
}
